package cn.youyeku.zsxh.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cn.youyeku.zsxh.app;

/**
 * Created by youye on 17-4-3.
 */

public abstract class SimpleAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    protected List<T> mDatas = new ArrayList<>();
    protected int mItemId;
    protected int lastPos = -1;
    protected SimpleAdapter(int mItemLayout) {
        this.mItemId = mItemLayout;

    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ListHolder(LayoutInflater.from(parent.getContext()).inflate(mItemId, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
       ListHolder listHolder = (ListHolder) holder;
        View item = listHolder.itemView;
        onBindData(listHolder,item,mDatas.get(position));
        setmOnItemClick(item, mDatas.get(position));

    }

    protected abstract void setmOnItemClick(View item, T data);

    protected abstract void onBindData(ListHolder listHolder, View item, T data);



    public void add(List<T> data){
        mDatas=data;
        if(mDatas.size()>0){
            notifyDataSetChanged();
        }else{
            app.backgroundThreadShortToast("没有数据");
        }
    }


    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    //进行控件绑定
    @SuppressWarnings("unchecked")
    protected class ListHolder extends RecyclerView.ViewHolder {


        public ListHolder(View itemView) {
            super(itemView);
        }

        public <T extends View> T getChildView(int id) {
            return (T) itemView.findViewById(id);
        }


    }

}
